/******************************************************************************
Tinapoc Project - The Java Reverse Engineering Toolkit
Copyright (C) 2005  Daniel Reynaud-Plantey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
******************************************************************************/

package tina.utils;

public class CommandLineParser {
    String arg[];

    public CommandLineParser(String arg[]) {
        this.arg = arg;
    }

// checks for the "--option" string and returns true if found
    public boolean isOptionSet(String option) {
        return getIndexOf(option)<0 ? false : true;
    }


// checks for the "-c" string and returns true if found
    public boolean isOptionSet(char c) {
        return getIndexOf(c)<0 ? false : true;
    }

// returns the index of "--option" in arg, -1 if not found
    public int getIndexOf(String option) {
        String s = (new StringBuilder("--")).append(option).toString();
        for(int i=0; i<arg.length; i++) {
            if(arg[i].equals(s))
                return i;
        }
        return -1;
    }

// returns the index of "-c" in arg, -1 if not found
    public int getIndexOf(char c) {
        String s = (new StringBuilder("-")).append(c).toString();
        for(int i=0; i<arg.length; i++) {
            if(arg[i].equals(s))
                return i;
        }
        return -1;
    }

// checks for the "--name" option and returns the next argument
// ex : "--file output.txt" -> getParameter("file") -> "output.txt"
// returns null if not found
    public String getParameter(String name) {
        int idx = getIndexOf(name);
        if(idx<0 || idx>=arg.length-1)
            return null;
        else
            return arg[idx];
    }


// checks for the "-c" option and returns the next argument
// ex : "-o output.txt" -> getParameter('o') -> "output.txt"
// returns null if not found
    public String getParameter(char c) {
        int idx = getIndexOf(c);
        if(idx<0 || idx>=arg.length-1)
            return null;
        else
            return arg[idx];
    }
}









